################################################################################ 
#
# The distribution of hate speech and its implications for content moderation
# PSRM - Replication package
# Figure 2
#
################################################################################# 

library(dplyr)
library(tidyr)
library(readr)
library(scales)
library(ggplot2)
library(cowplot)

rm(list = setdiff(ls(), ls(pattern = "^wd|^setsave$")))


################################################################################ 
#   Select outcomes
################################################################################ 

outvars <- c('anchor_deleted_12h', 'post_tweets_h_avg')
outvars_name <- c('Original Hate Tweet\nDeleted <12h', 'Probability of Hate Speech')


################################################################################ 
#   Make plot -BARPLOT
################################################################################ 

# lasso controls
container1 <- read_csv(paste0(wd_res, "/tables/tabE11.csv"))
container2 <- read_csv(paste0(wd_res, "/tables/tabE5.csv"))

container2$subset <- "Full Sample"
container2$yvar_name.f <- NULL
container2$treatments_lab.f <- NULL
container2$pvals_fdr <- NULL

container <- rbind.data.frame(container1, container2)
container <- container[which(container$yvar %in% outvars),]

container$yvar_name[container$yvar_name =='Probability of Hate Speech'] <- 'Probability of Hate Tweets'
container$yvar_name <- ifelse(
  container$yvar =='anchor_deleted_12h', 'Original Hate Tweet Deleted <12h',
  ifelse(container$yvar == 'hate_rate_85', 'Share of Hate Tweets', 
         ifelse(container$yvar == 'no_hate_tweets_post_85', 'Number of Hate Tweets', container$yvar_name))
)

container$subset[container$subset=="1/2 quantiles of Hate"] <- "Pre-treatment hate speech below median"
container$subset[container$subset=="2/2 quantiles of Hate"] <- "Pre-treatment hate speech above median"

container$yvar_name.f <- factor(container$yvar_name, levels=unique(container$yvar_name))
container$treatments_lab.f <- factor(container$treatments_lab, levels=unique(container$treatments_lab))
container$subset.f <- factor(container$subset, levels=c("Full Sample",
                                                       "Pre-treatment hate speech below median",
                                                       "Pre-treatment hate speech above median"))

d1 <- container %>%
    select(subset, treatments_lab, yvar_name, coef, se) %>%
    mutate(cii = coef - 1.96*se, cis = coef + 1.96*se) %>%
    mutate(subset2 = case_when(
        subset == "Pre-treatment hate speech below median" ~ "Hate speech below median",
        subset == "Pre-treatment hate speech above median" ~ "Hate speech above median",
        subset == "Full Sample" ~ "Full sample")) %>%
    mutate(subset2 = factor(subset2, levels = c("Hate speech below median", "Hate speech above median", "Full sample"))) %>%
    mutate(prereg = case_when(
        subset2 == "Full sample" ~ "Full sample",
        subset2 != "Full sample" ~ "Subsamples"     
    )) %>%
    mutate(prereg = factor(prereg, levels = c("Full sample", "Subsamples")))

colors <- hue_pal()(3) # "#F8766D" "#00BA38" "#619CFF"
my_palette <- c("Full sample" = "#619CFF", "Hate speech below median" = "#00BA38", "Hate speech above median" = "#F8766D")

g_all <- ggplot(data = d1) +
    aes(x = coef, y = treatments_lab, xmin = cii, xmax = cis, color = subset2, shape = subset2) +
    geom_vline(xintercept = 0, lty = 1, lwd = 0.2) +
    geom_pointrange(position = position_dodge2(0.6)) +
    scale_colour_manual( values = my_palette, breaks = c("Full sample", "Hate speech below median", "Hate speech above median")) +
    scale_shape_manual(values = c(16, 17, 4), breaks = c("Full sample", "Hate speech below median", "Hate speech above median")) +
    facet_grid(prereg ~ yvar_name) +
    labs(x = "", y = "") +
    guides(color = guide_legend(override.aes = list(shape = c(16, 17, 4))), shape = guide_legend(override.aes = list(color = my_palette))) +
    theme_light() +
    theme(strip.background = element_blank(), strip.text = element_text(color = "black", size = 11, face = "italic"), legend.title = element_blank(), legend.text = element_text(size = 10), axis.text = element_text(size = 10), legend.position = "bottom", panel.grid.minor = element_blank(), strip.text.y = element_blank())
g_all

ggsave(paste0(wd_res, "/figures/fig2.png"), g_all, width = 8, height = 4)

cat("\n====================\n")
cat("Saved Figure 2")
cat("\n====================\n")
